
using System;
using Oracle.DataAccess.Client;

namespace IP.Core.IPBusinessService
{
	/// <summary>
	/// CProvider chỉ tạo ra các đối tượng liên quan trực tiếp đến database như connection và adapter
	/// </summary>
	public class CProvider
	{
		public CProvider()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		
		#region Public Interface
			
		/// <summary>
		/// lấy một connection đã mở rồi
		/// </summary>
		/// <returns></returns>
		public static OracleConnection getConnection()
		{
			OracleConnection v_cn = new OracleConnection();
			m_str_connection_string = readConnectionString();
			v_cn.ConnectionString = m_str_connection_string;
			v_cn.Open();
			return v_cn;
		}
		public static string getDataSource(){
			return m_str_data_source;
		}
		public static string getUserName(){
			return m_str_user_name;
		}
		public static string getPassWord(){
			return m_str_password;
		}
		public static void SetConnectionParameter(
			string i_str_data_source
			,string i_str_user_name
			,string i_str_password
			){
			m_str_data_source = i_str_data_source;
			m_str_user_name = i_str_user_name;
			m_str_password = i_str_password;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public static OracleDataAdapter getAdapter(){
			return new OracleDataAdapter();
		}	
		
#endregion
		#region members
		protected static string m_str_connection_string  = "";
		protected static string m_str_data_source = "";
		protected static string m_str_user_name = "";
		protected static string m_str_password = "";
		#endregion
		#region Private Method
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		private static string readConnectionString(){			
			try {
				//Connection Information	
                m_str_data_source = System.Configuration.ConfigurationSettings.AppSettings["PRODUCT_DATASOURCE"].ToString();
                m_str_user_name = System.Configuration.ConfigurationSettings.AppSettings["USER_NAME"].ToString();
                m_str_password = System.Configuration.ConfigurationSettings.AppSettings["PASS_WORD"].ToString();
               
				string v_connection_string = 
					//username
					"User Id=" + m_str_user_name +
					//password
					";Password=" + m_str_password +
					//replace with your datasource value (TNSnames)
					";Data Source=" + m_str_data_source ;
				return v_connection_string;
			}
			catch(Exception v_e) {
				throw(v_e);
			}
		}
		#endregion
	}
}
